﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks;

namespace _010_RemotingClientTCPActivated
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpChannel channel = new TcpChannel();
                ChannelServices.RegisterChannel(channel, true);

                ActivatedClientTypeEntry entry = new ActivatedClientTypeEntry(typeof(_010_RemotingServerTCPActivated.RemotingServer), "tcp://localhost:9999");
                RemotingConfiguration.RegisterActivatedClientType(entry);

                _10_RemotingSharedAssembly.IServerInterface srv = new _010_RemotingServerTCPActivated.RemotingServer();


                while (true)
                {
                    Console.WriteLine("Enter array size:");
                    int size = Int32.Parse(Console.ReadLine());
                    if (size > 0)
                    {
                        _10_RemotingSharedAssembly.ServerRequest rq = new _10_RemotingSharedAssembly.ServerRequest();
                        rq.Array = new int[size];
                        for (int i = 0; i < size; i++)
                        {
                            Console.Write("Enter element {0}:", i);
                            rq.Array[i] = Int32.Parse(Console.ReadLine());
                        }
                        Console.Write("Sending request to server ...");
                        int rq_index = srv.AddRequest(rq);
                        Console.WriteLine("{0}", rq_index);

                        rq = srv.GetRequest(rq_index);
                        foreach (int a in rq.Array) Console.Write("{0} ", a);
                        Console.WriteLine();

                        Console.WriteLine("Processing request ...");
                        srv.ProcessRequest(rq_index);
                        Console.WriteLine("Done");

                        _10_RemotingSharedAssembly.ServerResponse resp = srv.GetResponse(rq_index);
                        foreach (int a in resp.Array) Console.Write("{0} ", a);
                        Console.WriteLine();
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}
